package cn.edu.cqupt.cxcy.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.cqupt.cxcy.dao.IndexDao;
import cn.edu.cqupt.cxcy.dto.IndexImpl;
import cn.edu.cqupt.cxcy.util.JsonUtil;
import cn.edu.cqupt.cxcy.util.Validate;


/**
 * 主页-【场地预约】
 * @author Dancer-mno
 * @date 2015年10月17日
 */
@SuppressWarnings("serial")
public class IndexNavFenye extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String html = req.getParameter("html"); //来自哪个页面
		double count = 1; //总记录数
		int page = Validate.stringIsNO(req.getParameter("page"))?Integer.parseInt(req.getParameter("page")):1; //当前页
		int type = Validate.stringIsNO(req.getParameter("type"))?Integer.parseInt(req.getParameter("type")):255; //类别
		IndexDao index = new IndexImpl();
		JsonUtil json = new JsonUtil(); 
		List<Object> lists = null;
		if("teac".equals(html)) {
			lists = index.listTeac( type, page, IndexDao.PAGING_TEAC);
			count  = Math.ceil( index.countTeac(type) / IndexDao.PAGING_TEAC);
		} else if ("inve".equals(html)) {
			lists = index.listInve( 255, page, IndexDao.PAGING_INVE);
			count  = Math.ceil( index.countInve(type) / IndexDao.PAGING_INVE);
		} else if("team".equals(html)) {
			lists = index.listTeam( page, IndexDao.PAGING_TEAM);
			count  = Math.ceil( index.countTeam() / IndexDao.PAGING_TEAM);
		} else if("pros".equals(html)) {
			lists = index.listPros( 1, page, IndexDao.PAGING_PROS);
			count  = Math.ceil( index.countPros( 255) / IndexDao.PAGING_PROS);
		} else if("news".equals(html)) {
			lists = index.listNews( type, page, IndexDao.PAGING_NEWS);
			count  = Math.ceil( index.countNews( 255) / IndexDao.PAGING_NEWS);
		} else if("cdyy".equals(html)) {
			HttpSession session = req.getSession();
			@SuppressWarnings("unchecked")
			List<Object> user= (List<Object>) session.getAttribute( "user");
			if(user != null) {
				lists = index.listCdyy(/*(String)user.get(4)*/"584819887@qq.com", page, IndexDao.PAGING_CDYY);
				count  = (int)Math.ceil( index.countCdyy( /*(String)user.get(4)*/"584819887@qq.com") / IndexDao.PAGING_CDYY);
			}
		}
		//校内导师 index_cyds?type=1 => < href="index_paging?html=teac&&type=1&&page=?">分页</a>
		//校外导师 index_cyds?type=2 => index_paging?html=teac&&type=2&&page=?
		//团队 index_team => index_paging?html=team&&page=?
		//项目 index_xmzs => index_paging?html=pros&&page=?
		//新闻 index_news => index_paging?html=news&&page=?
		//场地预约查看回执  (一定要登录,随便哪个都行) index_cdyy_Receipt => index_paging?html=cdyy&&page
		resp.setContentType("application/json;charset=utf-8");
		resp.setHeader("pragma","no-cache");
		resp.setHeader("cache-control","no-cache");
		json.put( "paging", lists); 
		json.put( "count", (int)count);
		PrintWriter out = resp.getWriter();
		out.println(json);
		out.flush();
		out.close();
	}


}
